package cn.t.web.device;

import cn.t.core.mode.Result;
import cn.t.dto.device.CargowayDTO;
import cn.t.dto.order.ExcelBatchOnShelfDTO;
import cn.t.facade.device.CargowayFacade;
import cn.t.listener.BatchOnShelfListener;
import cn.t.vo.device.CargowaySkuVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 设备管理 - 硬件配置
 *
 * @author t
 * @since 2024-05-24 10:15:13
 */
@Slf4j
@RestController
@RequestMapping("cargoway")
@AllArgsConstructor
public class CargowayController {

    private final CargowayFacade cargowayFacade;

    /**
     * 编辑货道硬件配置信息
     */
    @PostMapping("update")
    public Boolean update(@Valid @RequestBody CargowayDTO dto) {
        return this.cargowayFacade.update(dto);
    }

    /**
     * 删除货道
     */
    @PostMapping({"remove/{id}"})
    public Boolean remove(@PathVariable("id") Long id) {
        return this.cargowayFacade.removeById(id);
    }

    /**
     * 标签模板 - 导出sku标签
     */
    @PostMapping(value = "exportSkuLabel/{deviceId}")
    public void exportSkuLabel(HttpServletResponse response, @PathVariable("deviceId") Long deviceId) {
        this.cargowayFacade.exportSkuLabel(response, deviceId);
    }

    /**
     * 货道批量上架导入
     */
    @PostMapping(value = "/batchOnShelf/{deviceId}")
    public Result<String> batchOnShelf(@RequestPart("file") MultipartFile file, @PathVariable Long deviceId)  {
        // 解析导入数据
        List<ExcelBatchOnShelfDTO> excelData = this.cargowayFacade.checkAndGetExcelData(file, new BatchOnShelfListener());
        // 上架数据
        String importResultMsg = cargowayFacade.batchOnShelf(excelData, deviceId);
        return Result.success(importResultMsg);
    }

    /**
     * 聚合货道列表
     */
    @GetMapping("/list/{deviceId}")
    public List<CargowaySkuVO> list(@PathVariable Long deviceId, @RequestParam(required = false) String skuName,
                                    @RequestParam(required = false) String skuCode, @RequestParam(required = false) Boolean isAlert) {
        return this.cargowayFacade.list(deviceId, skuName, skuCode, isAlert);
    }

}

